package tech.horgee.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Null;
import lombok.Getter;
import lombok.Setter;
import tech.horgee.common.IWrapper;
import tech.horgee.group.InsertGroup;
import tech.horgee.group.UpdateGroup;
import tech.horgee.handler.PwdTypeHandler;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Objects;
import java.util.Set;

import static tech.horgee.common.MyConstants.SUPER_ADMIN_ID;

/**
 * 用户信息
 *
 * @author haojun
 * @since 2022-06-12
 */
@Getter
@Setter
@TableName(value = "tab_user", autoResultMap = true)
public class User implements Serializable, IWrapper<User> {

    private static final long serialVersionUID = 1L;

    /**
     * 主键
     */
    @NotNull(groups = UpdateGroup.class)
    @Null(groups = InsertGroup.class)
    @TableId(value = "user_id", type = IdType.AUTO)
    private Long userId;

    /**
     * 账号
     */
    @NotNull(groups = {InsertGroup.class})
    private String username;

    /**
     * 密码
     */
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    @TableField(typeHandler = PwdTypeHandler.class)
    private String password;

    /**
     * 姓名
     */
    private String nickname;

    /**
     * 手机
     */
    private String phone;

    /**
     * 邮箱
     */
    private String email;

    /**
     * 头像
     */
    private String avatar;

    /**
     * 是否冻结
     */
    private Boolean locked;

    /**
     * 乐观锁
     */
    @NotNull(groups = UpdateGroup.class)
    @Version
    private Long version;

    /**
     * 逻辑删除
     */
    @TableLogic
    private Boolean deleted;

    /**
     * 创建时间
     */
    @TableField(updateStrategy = FieldStrategy.NEVER)
    private LocalDateTime createTime;

    /**
     * 更新时间
     */
    @TableField(updateStrategy = FieldStrategy.NEVER)
    private LocalDateTime updateTime;

    /**
     * 角色ID集合
     */
    @TableField(exist = false)
    private Set<Long> roleIds;

    /**
     * 权限集合
     */
    @TableField(exist = false)
    private Set<String> permissions;

    @Override
    public LambdaQueryWrapper<User> wrapper() {
        return new LambdaQueryWrapper<User>()
                .eq(Objects.nonNull(userId), User::getUserId, userId)
                .like(Objects.nonNull(username), User::getUsername, username)
                .eq(Objects.nonNull(password), User::getPassword, password)
                .like(Objects.nonNull(nickname), User::getNickname, nickname)
                .like(Objects.nonNull(phone), User::getPhone, phone)
                .like(Objects.nonNull(email), User::getEmail, email)
                .eq(Objects.nonNull(locked), User::getLocked, locked)
                .eq(Objects.nonNull(version), User::getVersion, version)
                .eq(Objects.nonNull(deleted), User::getDeleted, deleted)
                .eq(Objects.nonNull(createTime), User::getCreateTime, createTime)
                .eq(Objects.nonNull(updateTime), User::getUpdateTime, updateTime)
                .ne(User::getUserId, SUPER_ADMIN_ID)
                .orderByDesc(User::getUserId)
                ;
    }
}
